<div id="i-warning-4" style="display: none;" class="warning_box">
</div>
<h2>Настройка поля: <?=$table?> &rarr; <?=$field ?></h2>
<input type="hidden" id="i-field-name" value=<?=$field?> />
<table>
	<!-- for all actions -->
	<tr>
		<td>Наименование поля:</td>
		<td><input style="width: 380px;" value="<?=(isset($data['alias']))?$data['alias']:''?>" class="text" id="i-alias" /></td>
	</tr>
	<!-- not implemented -->
	<tr style="display: none;">
		<td>Тип связи с иточником данных</td>
		<td>
			<select style="width: 380px;" disabled="disabled" >
				<option>без связи</option>
				<option>один к одному</option>
				<option>один ко многим</option>
				<option>многие ко многим</option>
			</select>
		</td>
	</tr>
	<tr style="display: none;">
		<td>Источник данных</td>
		<td>
			<select>
				<option>не выбран</option>
				<option>NOT IMPLEMENTED</option>
			</select>
		</td>
	</tr>
	<!-- /not implemented -->
	<!-- /for all actions -->
	<?php if (in_array('is_access_of_catalog', $access_actions)):?>
	<tr>
		<td>Отображать в каталоге</td>
		<td><input name="catalog" id="i-catalog" type="checkbox" <? if (isset($data['catalog']) && $data['catalog'] == 'true'): ?>checked="checked"<? endif; ?>></td>
	</tr>
	<tr id="i-sorting-tr" <? if (!isset($data['catalog']) || $data['catalog'] == 'false'): ?>style="display: none;<? endif;?>">
		<td>Сортировка по полю</td>
		<td><input id="i-enable-sorting"  <? if(isset($data['enable_sorting']) && $data['enable_sorting'] == 'true'): ?>checked="checked"<? endif;?> type="checkbox" ></td>
	</tr>
	<?php endif;?>
	<!-- not implemented -->
	<tr style="display: none;">
		<td>Метод отображения</td>
		<td>
			<select id="i-view-method" style="width: 380px;">
				<option value="" >не выбран</option>
				<? foreach ($edit_methods as $method): ?>
				<?php if(isset($data['input_method']) && $data['input_method'] == $method['option']): ?>
				<option selected="selected" value="<?=$method['option']?>" ><?=$method['option']?> — <?=$method['description']?></option>
				<?php else: ?>
				<option value="<?=$method?>" ><?=$method?></option>
				<?php endif ?>
				<? endforeach; ?>
			</select>
		</td>
	</tr>
	<!-- / not implemented -->
	<!-- /if catalog -->
	<!-- if edit/add -->
	<?php if (in_array('is_access_to_editing', $access_actions) || in_array('is_access_to_create', $access_actions)):?>
	<tr>
		<td>Метод редактирования</td>
		<td>
			<select id="i-input-method" style="width: 380px;">
				<option value="" >не выбран</option>
				<option value="None" >None</option>
				<? foreach ($edit_methods as $method): ?>
				<?php if(isset($data['input_method']) && $data['input_method'] == $method['option']): ?>
				<option selected="selected" value="<?=$method['option']?>" ><?=$method['option']?> — <?=$method['description']?></option>
				<?php else: ?>
				<option value="<?=$method?>" ><?=$method?></option>
				<?php endif ?>
				<? endforeach; ?>
			</select>
		</td>
	</tr>
	<?php endif;?>
	<tr>
		<td></td>
	</tr>
	<!-- /if edit/add -->
	<tr>
		<td></td>
		<td><input id="i-ok" type="submit" value="Применить"></td>
	</tr>
</table>
<script type="text/javascript">
$('#i-catalog').change(function(){
	if($(this).is(':checked')){
		$('#i-sorting-tr').css('display','');
	}else{
		$('#i-sorting-tr').css('display','none');
	}
});

$('#i-ok').click(function(){

	$.get(
		'/curd/pool_adding',
		{
			'uid' : $('#i-uid').val(),
			'state' : 2,
			'alias' : $('#i-alias').val(),
			'input_method' : $('#i-input-method').val(),
			'catalog' : $('#i-catalog').is(':checked'),
			'enable_sorting' : $('#i-enable-sorting').is(':checked'),
			'field_name' : $('#i-field-name').val()
		},
		function (data) {
			console.log(data);
			var answer = $.parseJSON(data);
			if(answer.is_error == 1){
				if (answer.error_message == '_reload_') {
					window.location.reload();
					return;
				}
				$('#i-warning-4')
				.css('display','')
				.html(answer.error_message);
				return;
			}

			$('#i-<?=$field?>').html('<span style="color:green;">ОК</span>');
			$(".ajax").fancybox({autoDimensions:false});
			$.fancybox.close();

			var is_valid = true;
			$('.c-is-ok').each(function(){
				if ('<span style="color:green;">ОК</span>' != $(this).html()) {
					console.log('test');
					is_valid = false;
				} else {
					console.log('test');
				}
				console.log($(this).html());
			});

			if (is_valid) {
				$('#i-step3-button').attr('disabled', undefined);
			}
		}
	);

});
</script>